package pool

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"user-center/config"
)

var db *gorm.DB

func init() {
	host := config.GetConfig().MySQL.Host
	port := config.GetConfig().MySQL.Port
	account := config.GetConfig().MySQL.Account
	password := config.GetConfig().MySQL.Password
	dbname := config.GetConfig().MySQL.DBName

	dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", account, password, host, port, dbname)
	var err error

	db, err = gorm.Open(mysql.Open(dns), &gorm.Config{})
	if err != nil {
		panic("链接数据库失败，error=" + err.Error())
	}
	sqlDB, _ := db.DB()
	//设置数据库连接池的最大连接数
	sqlDB.SetMaxIdleConns(20)
	//连接池最大允许的空闲连接数
	sqlDB.SetMaxOpenConns(100)
}

func GetDB() *gorm.DB {
	return db
}
